/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.room.scene.web;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.util.time.DateUtils;
import com.jeesite.modules.conf.RedisUtils;
import com.jeesite.modules.zx.examination.room.entity.TexaminationRoom;
import com.jeesite.modules.zx.examination.room.scene.entity.TexaminationRoomScene;
import com.jeesite.modules.zx.examination.room.scene.service.TexaminationRoomSceneService;

/**
 * 考试考场场次Controller
 * @author zpp
 * @version 2020-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/room/scene/texaminationRoomScene")
public class TexaminationRoomSceneController extends BaseController {

	@Autowired
	private TexaminationRoomSceneService texaminationRoomSceneService;
	@Autowired
	private RedisUtils redisUtils;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TexaminationRoomScene get(String id, boolean isNewRecord) {
		return texaminationRoomSceneService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:room:scene:view")
	@RequestMapping(value = {"list", ""})
	public String list(TexaminationRoomScene texaminationRoomScene, Model model) {
		model.addAttribute("texaminationRoomScene", texaminationRoomScene);
		return "modules/zx/examination/room/scene/texaminationRoomSceneList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:room:scene:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TexaminationRoomScene> listData(TexaminationRoomScene texaminationRoomScene, HttpServletRequest request, HttpServletResponse response) {
		texaminationRoomScene.setPage(new Page<>(request, response));
		Page<TexaminationRoomScene> page = texaminationRoomSceneService.findPage(texaminationRoomScene);
		return page;
	}
	
	@RequestMapping(value = "findList")
	@ResponseBody
	public List<TexaminationRoomScene> findList(TexaminationRoomScene texaminationRoomScene, HttpServletRequest request, HttpServletResponse response) {
		String examId=texaminationRoomScene.getExamId();
		if(StringUtils.isBlank(examId)){
			return new ArrayList<TexaminationRoomScene>();
		}
		String roomId = texaminationRoomScene.getRoomId();
		if(StringUtils.isBlank(roomId)){
			return new ArrayList<TexaminationRoomScene>();
		}
		List<TexaminationRoomScene> page = texaminationRoomSceneService.findList(texaminationRoomScene);
		return page;
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:room:scene:view")
	@RequestMapping(value = "form")
	public String form(TexaminationRoomScene texaminationRoomScene, Model model) {
		model.addAttribute("texaminationRoomScene", texaminationRoomScene);
		return "modules/zx/examination/room/scene/texaminationRoomSceneForm";
	}

	/**
	 * 保存考试考场场次
	 */
	@RequiresPermissions("zx:examination:room:scene:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TexaminationRoomScene texaminationRoomScene) {
		texaminationRoomSceneService.save(texaminationRoomScene);
		try {
			String id=texaminationRoomScene.getId();
			if(StringUtils.isNotBlank(id)) {
					Date endDate = texaminationRoomScene.getEndDate();
					LocalDateTime date2LocalDateTime = DateUtils.date2LocalDateTime(endDate);
					LocalDateTime now=LocalDateTime.now();
					Long betweenMillis = DateUtils.betweenMillis(now,date2LocalDateTime);
					if(betweenMillis>0) {
					  redisUtils.set(id, endDate, betweenMillis);
					}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("保存考试考场场次成功！"));
	}
	
	/**
	 * 停用考试考场场次
	 */
	@RequiresPermissions("zx:examination:room:scene:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TexaminationRoomScene texaminationRoomScene) {
		texaminationRoomScene.setStatus(TexaminationRoomScene.STATUS_DISABLE);
		texaminationRoomSceneService.updateStatus(texaminationRoomScene);
		return renderResult(Global.TRUE, text("停用考试考场场次成功"));
	}
	
	/**
	 * 启用考试考场场次
	 */
	@RequiresPermissions("zx:examination:room:scene:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TexaminationRoomScene texaminationRoomScene) {
		texaminationRoomScene.setStatus(TexaminationRoomScene.STATUS_NORMAL);
		texaminationRoomSceneService.updateStatus(texaminationRoomScene);
		return renderResult(Global.TRUE, text("启用考试考场场次成功"));
	}
	
	/**
	 * 删除考试考场场次
	 */
	@RequiresPermissions("zx:examination:room:scene:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TexaminationRoomScene texaminationRoomScene) {
		texaminationRoomSceneService.delete(texaminationRoomScene);
		try {
			redisUtils.del(texaminationRoomScene.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("删除考试考场场次成功！"));
	}
	
}